package org.germinus.cardea.MuleServiceBundle.impl;

import java.util.ArrayList;

import models_bundle.Medicine;
import models_bundle.MedicineUnit;

import org.germinus.cardea.config.GetConfiguration;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;

import sipbundle.service.SIPBundle_interface;


public class ContextBean {
	static private BundleContext bc;
	static private StockConsumer consumer;
	private static ContextBean cBean= null;
	//private FirstService service=null;
	
	private ContextBean(){
		bc=null;
	}
	
	static public ContextBean getContextBean(){
		if(cBean==null){
			System.out.println("El bean es null, se crea un bean");
			cBean = new ContextBean();
		}
		return cBean;
	}
	
	public void setContext(BundleContext context){
		if(bc==null){
			System.out.println("El bc es null, se estabece el bundlecontext");
			bc=context;
		}
		return;
	}
	
	public void stopContext(){
		bc=null;
		return;
	}
	
	public void setConsumer(StockConsumer sc){
		if(consumer==null){
			consumer=sc;
		}
		return;
	}
	
	public StockConsumer getConsumer(){
		return consumer;
	}

	public int getStock(String typeMedicine){
		
		int stock=-1;
		
		//Codigo para el bundle real de RFID
		/*
		 *  Importar en el Manifest el paquete models_bundle
		 */ 
				Medicine medtype;
				try {
					medtype = Medicine.findByName(typeMedicine);
					//ArrayList<MedicineUnit> medlist=medtype.medicineUnits();
					ArrayList<MedicineUnit> medlist=(ArrayList<MedicineUnit>) medtype.medicineUnits(false);
					stock=medlist.size();
					System.out.println("Using RFIDService: stock: " + stock);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
		 
		
		//Llamo al RFID para que me de el stock
		/*if(bc!=null){
			ServiceReference reference = bc.getServiceReference(ServiceRFID.class.getName());
			if(reference!=null){
				ServiceRFID service = (ServiceRFID)bc.getService(reference);
				stock=service.getStock(typeMedicine);
				Activator.log("Consulta de stock");
				System.out.println("Using RFIDService: stock: " + stock);
				bc.ungetService(reference);
			}else{
				System.out.println("No Service Available");
			}
		}
		if(stock<0){
			stock=stock*-1;
		}*/
		return stock;
		
	}
	
	public void sendMessage(String message){
		
		String destination=GetConfiguration.getString("ContextBean.SIPDestination");
		
		//Codigo para el bundle SIP original
		/*
		 * 
		 * Importar en el MANIFEST el paquete sipbundle.service
		 */
		
		 if(bc!=null){
			ServiceReference[] reference;
			try {
				reference = bc.getServiceReferences(SIPBundle_interface.class.getName(), "(Type=*)");
				if(reference!=null){
					SIPBundle_interface service = (SIPBundle_interface)bc.getService(reference[0]);
					service.send("ALARMA", message, destination);
					bc.ungetService(reference[0]);
				}else{
					System.out.println("No SIP Service Available");
				}
			} catch (InvalidSyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		 }
		 
		
		//codigo para acceder al bundle SIP y mandar el mensage
		System.out.println("Sending message to pharmacy staff...");
		return;
	}

}
